package com.example.administrator.teainfo.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.example.administrator.teainfo.Bean.URLResouce;
import com.example.administrator.teainfo.R;
import com.example.administrator.teainfo.adapter.MyListViewAdapter;
import com.example.administrator.teainfo.utils.AsyncTaskHelper;
import com.example.administrator.teainfo.utils.JsonHelper;
import com.example.administrator.teainfo.widget.MyViewPager;

import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContentFragment extends Fragment implements View.OnClickListener{
    private ListView listView;
    private AsyncTaskHelper asyncTaskHelper;
    private MyListViewAdapter adapter;
    private RelativeLayout relativeLayout;
    private MyViewPager myViewPager;
    private List<ImageView> imageViews;
    private RadioGroup radioGroup_fragment;

    public ContentFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        listView  = (ListView) view.findViewById(R.id.fragment_content_lv);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.fragment_content_relativeLayout);
        myViewPager = (MyViewPager) view.findViewById(R.id.fragment_content_myViewPager);
        radioGroup_fragment = (RadioGroup) view.findViewById(R.id.radioGroup_fragment);
        initData();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {//实现上拉加载
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        return view;
    }

    private void initData() {
        asyncTaskHelper = new AsyncTaskHelper();
        Bundle bundle = getArguments();
        int type = bundle.getInt("type",0);
        String url = "";
        switch (type){
            case 0://头条
                url = URLResouce.HEADLINE_URL;
//                imageViews = new ArrayList<>();
//
//			/* 开始加载轮播广告数据 */
////                new MyTask(0).execute(MyConfig.JSON_URL);
//                relativeLayout
//                        .setLayoutParams(new LinearLayout.LayoutParams(
//                                ViewGroup.LayoutParams.MATCH_PARENT, getButtonViewPager()));
//                imageViews = new ArrayList<ImageView>();
//                for (int i = 0; i < 3; i++) {
//                    ImageView imageView = new ImageView(getActivity());
//                    imageView.setImageResource(R.mipmap.ic_launcher);
//                    imageView.setOnClickListener(this);
//                    imageViews.add(imageView);
//                }
//                HeadNewsAdapter adapter = new HeadNewsAdapter(imageViews);
//                myViewPager.setAdapter(adapter);
//			/* 页面滑动时的监听 */
//                myViewPager
//                        .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//
//                            @Override
//                            public void onPageSelected(int arg0) {
//                                radioGroup_fragment.getChildAt(arg0).performClick();
//                            }
//
//                            @Override
//                            public void onPageScrolled(int arg0, float arg1,
//                                                       int arg2) {
//
//                            }
//
//                            @Override
//                            public void onPageScrollStateChanged(int arg0) {
//
//                            }
//                        });
//			/* 创建若干单选按钮，被把按钮放到组里 */
//                for (int i = 0; i < radioGroup_fragment.getChildCount(); i++) {
//                    // RadioButton rButton =new RadioButton(getActivity());// 创建单选按钮
//                    RadioButton rButton = (RadioButton) radioGroup_fragment
//                            .getChildAt(i);
//                    rButton.setTag(i);// 设置tag的下标与滑动界面的下标保持一至
//                    // rButton.setLayoutParams(new LinearLayout.LayoutParams(15,
//                    // 15));// 设置点的宽高
//                    rButton.setBackgroundResource(R.drawable.slide_image_dot_c2);//
//                    // 背景状态设置
//                    rButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));// 去掉单选按钮前面的点颜色为空
//                    // radioGroup_fragment.addView(rButton);
//                }
//			/* 单选按钮组被选中的监听 */
//                radioGroup_fragment
//                        .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//                            @Override
//                            public void onCheckedChanged(RadioGroup group,
//                                                         int checkedId) {
//                                // Log.i(TAG, "==得到按钮，并改变当前按钮显示的颜色");
//                                RadioButton rButton = (RadioButton) group
//                                        .findViewById(checkedId);
//
//                                // Log.i(TAG, "==得到相应的tag值(tag值就是下标)");
//                                int index = (Integer) (rButton.getTag());
//
//                                // Log.i(TAG, "拿出按钮中的tag值，就是下标 index==" + index);
//                                myViewPager.setCurrentItem(index);
//                            }
//                        });
//                radioGroup_fragment.getChildAt(0).performClick();// 默认设置第一个页面是单击状态
//
//


//                //上方广告
//                new AsyncTaskHelper().downloadData(URLResouce.HOME_URL, new AsyncTaskHelper.OnDataDownloadListener() {
//                    @Override
//                    public void onDataDownload(byte[] result) {
//                        String json = new String(result);
//                        Log.e(TAG, "onDataDownload: 图片的byte[]："+json);
//                        List<Map<String, Object>> data = JsonHelper.jsonStringToList(json, new String[]{"image"}, "data");
//                        Log.e(TAG, "onDataDownload: "+data.get(0).get("image"));
//                        String[] image_url = new String[data.size()];
//                        for (int i = 0; i < data.size(); i++) {
//                            image_url[i] = (String) data.get(i).get("image");
//                        }
////                        Log.e(TAG, "onDataDownload: 图片的urls："+image_url[0]);
//                        ImageDownloadHelper helper = new ImageDownloadHelper();
//                        for (int i = 0; i < image_url.length; i++) {
//                            final ImageView imageView  = new ImageView(getContext());
//                            imageView.setLayoutParams(new MyViewPager.LayoutParams());
//                            imageView.setTag(image_url[i]);
//                            helper.myDownloadImage(getContext(), image_url[i], imageView, new ImageDownloadHelper.OnImageDownloadListener() {
//                                @Override
//                                public void onImageDownload(Bitmap bitmap, String imgUrl) {
//                                    View view = imageView.findViewWithTag(imgUrl);
//                                    if (bitmap != null&&view instanceof ImageView) {
//                                        imageView.setImageBitmap(bitmap);
//                                    }
//                                }
//                            });
//                            imageViews.add(imageView);
//                        }
//                    }
//                });
//                myViewPager.setAdapter(new HeadNewsAdapter(imageViews));//上方广告的
                relativeLayout.setVisibility(View.GONE);
                break;
            case 1://百科
                url = URLResouce.CHANNEL_URL+URLResouce.TYPE_ENCYCLOPEDIA;
                relativeLayout.setVisibility(View.GONE);
                break;
            case 2://资讯
                url = URLResouce.CHANNEL_URL+URLResouce.TYPE_INFOMATION;
                relativeLayout.setVisibility(View.GONE);
                break;
            case 3://经营
                url = URLResouce.CHANNEL_URL+URLResouce.TYPE_MANAGEMENT;
                relativeLayout.setVisibility(View.GONE);
                break;
            case 4://数据
                url = URLResouce.CHANNEL_URL+URLResouce.TYPE_DATA;
                relativeLayout.setVisibility(View.GONE);
                break;
        }
        asyncTaskHelper.downloadData(url, new AsyncTaskHelper.OnDataDownloadListener() {
            @Override
            public void onDataDownload(byte[] result) {
                String json =  new String(result);
                Log.e(TAG, "onDataDownload: "+json);
                List<Map<String, Object>> data = JsonHelper.jsonStringToList(json, new String[]{"title", "source",
                        "nickname", "create_time", "wap_thumb", "id"}, "data");
                Log.e(TAG, "onDataDownload: "+data.get(0));
                adapter = new MyListViewAdapter(getContext(),data);
//                adapter.addList(data);
                adapter.notifyDataSetChanged();
            }
        });
    }

    public int getButtonViewPager() {
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        int lenButton = 0;
        lenButton = display.getWidth() * 1 / 2;
        return lenButton;
    }

    @Override
    public void onClick(View v) {

    }
}
